package com.adams.action.sys;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;

import com.adams.access.db.bean.Icon;
import com.adams.access.db.bean.Menu;
import com.adams.common.BaseAction;
import com.adams.common.Certificate;
import com.adams.common.Constants;
import com.adams.custom.page.util.ViewConstant;
import com.adams.sys.icon.service.IconService;
import com.adams.sys.menu.service.MenuService;
import com.adams.sys.menu.service.impl.MenuServiceImpl;

/**
 * 
 * 
 * File Name : IndexAction.java
 * 
 * @Description : 首页处理类
 * @author 902990封文进
 */
public class IndexAction extends BaseAction
{

	@Autowired
	private MenuService menuService;

	@Autowired
	private IconService iconService;

	private String xtype = "";

	private Long menuId;

	/**
	 * 
	 * Description : 显示菜单
	 * 
	 * @return String
	 * 
	 */
	public String listMenu()
	{
		if (xtype.equals("dynamicTbar"))
		{// 一级菜单
			listTopMenu();
		} else
		{ // 二级菜单
			listSubMenu();
		}
		return null;
	}

	private void listSubMenu()
	{
		// 兼容前端传入null值的问题
		if (menuId == null)
		{
			List<Menu> list = menuService.querySubMenus(Long.valueOf("0"));
			for (Menu menu : list)
			{
				if (visible(menu))
				{
					menuId = menu.getId();
					break;
				}
			}

		}
		List<Menu> list = menuService.querySubMenus(menuId);

		List<Map> result = new ArrayList<Map>();
		List<Menu> menuList;
		List<Map> childResult;
		Map map;
		Map childMap;
		for (Menu menu : list)
		{
			if (!visible(menu))
			{
				continue;
			}
			map = new HashMap();
			result.add(map);
			setChildMenu(menu, map);

			menuList = menuService.querySubMenus(menu.getId());
			if (menuList != null && menuList.size() > 0)
			{
				childResult = new ArrayList<Map>();
				for (Menu childMenu : menuList)
				{
					if (!visible(childMenu))
					{
						continue;
					}
					childMap = new HashMap();
					childResult.add(childMap);
					setChildMenu(childMenu, childMap);
				}
				map.put("children", childResult);
			}
		}
		write(JSONArray.fromObject(result).toString());
	}

	private void setChildMenu(Menu menu, Map map)
	{
		map.put("id", menu.getId());
		map.put("xtype", xtype);
		map.put("text", menu.getMenuName());
		map.put("tips", menu.getTips());
		map.put("expanded", menu.isExpanded());
		
		if (menu.getUrl().indexOf("?") >= 0)
		{
			map.put("action", menu.getUrl() + "&menuId=" + menu.getId());
		} else
		{
			map.put("action", menu.getUrl() + "?menuId=" + menu.getId());
		}

		List<Menu> menuList = menuService.querySubMenus(menu.getId());
		if (menuList != null && menuList.size() > 0)
		{
			map.put("leaf", false);

		} else
		{
			map.put("leaf", true);
		}

		Icon icon = iconService.queryById(Long.valueOf(menu.getIconId()));
		if (icon != null)
		{
			map.put("icon", icon.getUrl());
		}
		map.put("cls", "tree-margin-height");

	}

	private void listTopMenu()
	{
		List<Menu> list = menuService.querySubMenus(Long.valueOf("0"));
		List<Map> result = new ArrayList<Map>();
		List<Menu> menuList;
		Icon icon;
		for (Menu menu : list)
		{
			if (!visible(menu))
			{
				continue;
			}
			Map map = new HashMap();
			map.put("id", menu.getId());
			map.put("xtype", xtype);
			map.put("text", menu.getMenuName());
			map.put("tips", menu.getTips());

			if (menu.getUrl().indexOf("?") >= 0)
			{
				map.put("action", menu.getUrl() + "&menuId=" + menu.getId());
			} else
			{
				map.put("action", menu.getUrl() + "?menuId=" + menu.getId());
			}
			icon = iconService.queryById(Long.valueOf(menu.getIconId()));
			if (icon != null)
			{
				map.put("icon", icon.getUrl());
			}

			map.put("actionType", "reload_menu");

			menuList = menuService.querySubMenus(menu.getId());
			if (menuList != null && menuList.size() > 0)
			{
				map.put("leaf", false);
			} else
			{
				map.put("leaf", true);
			}
			result.add(map);
		}
		Map map = new HashMap();
		map.put("tbar", result);
		map.put("wins", "");
		map.put(ViewConstant.JSON_DEAL_SUCESS, true);
		write(JSONObject.fromObject(map).toString());
	}

	/**
	 * 
	 * Description : 控制菜单是否可见
	 * 
	 * @param rightList
	 * @param menu
	 * @return
	 * 
	 */
	private boolean visible(Menu menu)
	{
		if (Constants.SUPER_USER_NAME.equals(getUserName()))
		{
			return true;
		}
		// 获取权限
		List<Menu> rightList = Certificate.getRights(request);

		if (!menu.getVisible())
		{
			return false;
		}
		for (Menu m : rightList)
		{
			if (m.getMenuCode().equals(menu.getMenuCode()))
			{
				return true;
			}
		}
		return false;
	}

	public void setMenuService(MenuServiceImpl menuService)
	{
		this.menuService = menuService;
	}

	public void setIconService(IconService iconService)
	{
		this.iconService = iconService;
	}

	public void setXtype(String xtype)
	{
		this.xtype = xtype;
	}

	public void setMenuId(Long menuId)
	{
		this.menuId = menuId;
	}
}
